package com.hsd.app.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsd.app.model.HsdFbInfo;
import com.hsd.app.service.IDataManageService;
import com.hsd.commons.model.HsdAccount;
import com.hsd.core.page.entity.PageParams;
import com.hsd.core.page.entity.Pagination;
import com.hsd.core.utils.TimeUtil;


@Controller
@RequestMapping("/datamanage")
public class FbManageAction extends PLBaseAction{
	@Autowired
	private IDataManageService hpdataService;
	
	/**
	 * 数据管理 ：加载食药材信息列表
	 * @param pageParams
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/loadFbList.do")
	public String loadFbList(PageParams pageParams,HttpServletRequest request,ModelMap model){
		String fbname = request.getParameter("fbname"); 
		Pagination page = hpdataService.loadFbList(pageParams,fbname);
		
		model.addAttribute("page",page);
		model.addAttribute("fbname",fbname); 
		return "app/data/data-fb-list";
	} 
	/**
	 * 新增一个食药材
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/addOneFb.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String addOneFb(HsdFbInfo fbobj,HttpServletRequest request,ModelMap model){
		Map query = new HashMap();
		query.put("fbname", fbobj.getFbname());
		Object existData = hpdataService.getUniqueObject(HsdFbInfo.class, query);
		if(null != existData){
			return "该食材已存在！";
		}
		fbobj.setId(this.hpdataService.getSeqNextVal("seq_hsd_fb_info_id"));
		HsdAccount accInfo = this.getCurrentuser(request); 
		fbobj.setYouxiaobj(PLCONSTANTS.YOUXIAOBJ_YX);
		fbobj.setOpertime(TimeUtil.getCurrentDate());
		if(null != accInfo){
			fbobj.setUserid(accInfo.getId());
			fbobj.setUsername(accInfo.getUsername());
		}
		hpdataService.save(fbobj);  
		return "保存成功！";
	}
	/**
	 * 查询一个食药材
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/selectOneFb.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String selectOneFb(HttpServletRequest request,ModelMap model){ 
		 String id = request.getParameter("id");
		 HsdFbInfo fbinfo = (HsdFbInfo) this.hpdataService.findByID(HsdFbInfo.class, new Long(id));
		 model.put("fbinfo", fbinfo);
		 return "修改成功！";
	}
	/**
	 * 修改一个食药材
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/editOneFb.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String editOneFb(HsdFbInfo fbobj,HttpServletRequest request,ModelMap model){ 
		hpdataService.save(fbobj);
		return "修改成功！";
	}
	
	
	/**
	 * 删除食药材
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/delOneFb.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String delOneFb(HttpServletRequest request,ModelMap model){
		String id = request.getParameter("id"); 
		HsdFbInfo fbObj = (HsdFbInfo)hpdataService.findByID(HsdFbInfo.class, new Long(id));
		fbObj.setYouxiaobj(PLCONSTANTS.YOUXIAOBJ_WX);
		hpdataService.saveObject(fbObj);
		return "删除成功！";
	}
	
	/**
	 * 食药材查询
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/findFbList.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String findFbList(HttpServletRequest request,ModelMap model){
		String fbname = request.getParameter("fbname"); 
		JSONObject fbListData = this.hpdataService.getFbDataByname(fbname);
		return fbListData.toString(); 
	}
	/**
	 * 穴位经络查询
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/findMdList.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String findMdList(HttpServletRequest request,ModelMap model){
		String mdname = request.getParameter("mdname"); 
		JSONObject mdListData = this.hpdataService.getMdDataByname(mdname);
		return mdListData.toString(); 
	}
	/**
	 * 病症证查询
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/findDssList.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String findDssList(HttpServletRequest request,ModelMap model){
		String dssname = request.getParameter("dssname"); 
		JSONObject dssListData = this.hpdataService.getDssDataByname(dssname);
		return dssListData.toString(); 
	}
	/**
	 * 功效信息查询
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/findEffectList.do",produces = {"application/json;charset=UTF-8"})
	public @ResponseBody String findEffectList(HttpServletRequest request,ModelMap model){
		String effectname = request.getParameter("effectname"); 
		JSONObject effectListData = this.hpdataService.getEffectDataByname(effectname);
		return effectListData.toString(); 
	}
}
